$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tcmember/list',
        datatype: "json",
        colModel: [			
			/*{ label: 'id', name: 'id', index: 'ID', width: 50, key: true },*/
			{ label: '会员电话', name: 'phone', index: 'PHONE', width: 90 },
			{ label: '会员真实姓名', name: 'realName', index: 'REAL_NAME', width: 85 },
			{ label: '会员性别', name: 'memberSex', index: 'MEMBER_SEX', width: 70,formatter: function(value, options, row){
					if(value == "MALE"){
						return '<span class="label label-primary">男</span>'
					}else {
						return '<span class="label label-warning">女</span>'
					}}},
			{ label: '会员EMAIL', name: 'memberEmail', index: 'MEMBER_EMAIL', width: 135 },
			{ label: '会员类型', name: 'memberType', index: 'MEMBER_TYPE', width: 70,formatter: function(value, options, row){
					if(value == "DUTY_MANAGER"){
						return '<span class="label label-success">值班经理</span>'
					}else if(value == "VERIFIED_MEMBER"){
						return '<span class="label label-success">认证会员</span>'
					}else if(value == "REGULAR_MEMBER"){
						return '<span class="label label-success">普通会员</span>'
					}else{
						return '<span class="label label-success">游客</span>'
					}}},
			/*label: '状态', name: 'status', width: 60, formatter: function(value, options, row){
		return value === 0 ?
			'<span class="label label-danger">禁用</span>' :
			'<span class="label label-success">正常</span>';*/
			{ label: '会员星级', name: 'memberLevel', index: 'MEMBER_LEVEL', width: 70,formatter: function(value, options, row){
					if(value == "5"){
						return '<span class="label label-info">★★★★★</span>'
					}else if(value == "4"){
						return '<span class="label label-info">★★★★</span>'
					}else if(value == "3"){
						return '<span class="label label-info">★★★</span>'
					}else if(value == "2"){
						return '<span class="label label-info">★★</span>'
					}else if(value == "1"){
						return '<span class="label label-info">★</span>'
					}else{
						return '<span class="label label-info"></span>'
					}


			}},
			{ label: '会员卡号', name: 'memberCardNo', index: 'MEMBER_CARD_NO', width: 133 },
			/*{ label: '二维码文件路径 (基于会员编号生成) ', name: 'qrCodeFilePath', index: 'QR_CODE_FILE_PATH', width: 80 },*/
			/*{ label: '合伙人（PARTNER:是合伙人;NOT_PARTNER:不是合伙人）', name: 'partnerProperty', index: 'PARTNER_PROPERTY', width: 80 },
			{ label: '推荐会员编号', name: 'recommendMemberNo', index: 'RECOMMEND_MEMBER_NO', width: 80 }, 			
			{ label: '用户语言（需授权获得）', name: 'memberLanguage', index: 'MEMBER_LANGUAGE', width: 80 }, */
			{ label: '会员使用状态', name: 'useStatus', index: 'USE_STATUS', width: 85 ,formatter: function(value, options, row){
					if(value == "NORMAL"){
						return '<span class="label label-primary">正常</span>';
					}else if(value == "FORBIDDEN"){
						return '<span class="label label-danger">禁用</span>';
					}
				}},
			{ label: '会员状态', name: 'memberStatus', index: 'MEMBER_STATUS', width: 85 ,formatter: function(value, options, row){
					if(value == "VERIFY_SUCCESS"){
						return '<span class="label label-success">会员认证成功</span>';
					}else if(value == "BACKGROUND_VERIFY_FAIL"){
						return '<span class="label label-danger">后台认证失败</span>';
					}else if(value == "VERIFY_APPLIED"){
						return '<span class="label label-info">会员申请已提交</span>';
					}else if(value == "REGISTER_SUCCESS"){
						return '<span class="label label-success">会员注册成功</span>';
					}else{
                        return '<span class="label label-success">游客</span>';
                    }
				}},
			/*{ label: '订阅状态 授权获得,NOT_SUBSCRIBE:表示没有关注，拉不到其他信息；SUBSCRIBED:表示关注，可以拉到其他授权信息', name: 'subscribeStatus', index: 'SUBSCRIBE_STATUS', width: 80 }, */
			/*{ label: '会员总积分值', name: 'totalIntegral', index: 'TOTAL_INTEGRAL', width: 80 },
			{ label: '会员总府宝值', name: 'totalCredit', index: 'TOTAL_CREDIT', width: 80 }, 	*/
			/*{ label: '国家', name: 'country', index: 'COUNTRY', width: 80 },
			{ label: '省', name: 'province', index: 'PROVINCE', width: 80 }, 			
			{ label: '城市', name: 'city', index: 'CITY', width: 80 }, 			
			{ label: '工作单位', name: 'company', index: 'COMPANY', width: 80 },*/
			/*{ label: '乐观锁', name: 'version', index: 'VERSION', width: 80 }, */
			{ label: '备注', name: 'remark', index: 'REMARK', width: 80 }, 			
			/*{ label: '关注时间', name: 'subscribeTime', index: 'SUBSCRIBE_TIME', width: 80 },
			{ label: '取消关注时间', name: 'cancelSubscribeTime', index: 'CANCEL_SUBSCRIBE_TIME', width: 80 }*/
			/*{label: '创建时间', name: 'createTime', index: 'CREATE_TIME', width: 80 },*/
			/*{ label: '更新时间', name: 'updateTime', index: 'UPDATE_TIME', width: 80 }, */
			{ label: '注册时间', name: 'registerTime', index: 'REGISTER_TIME', width: 80 }
        ],
		viewrecords: true,
        height: 580,
        rowNum: 50,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			typeNo:null,
			memberName:null
		},
		showList: true,
		title: null,
		tcMember: {recommendMemberNo:null}
	},
	methods: {
		showMemberPage: function(event){
			layer.open({
				type: 2,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "选择会员",
				area: ['1100px', '500px'],
				shade: 0,
				shadeClose: true,
				content: 'http://localhost:8080/renren-admin/modules/sys/tcmember_load.html',
				btn: ['确定', '取消'],
				btn1: function (index,layero) {
					var ibody = layer.getChildFrame ('body',index); //获取iframe页面body
					var tr = ibody.find("#jqGrid tr[class*='success']");
					var recommendMemberNo = tr.find("td:eq(8)").text();
					if(tr.length>1){
						alert("只能选择一名会员");
						return;
					} else if(tr.length==0){
						alert("请选择一名会员");
						return;
					}

					/*alert(memberName);*/
					vm.tcMember.recommendMemberNo=recommendMemberNo;
					$("#recommendMemberNo").val(recommendMemberNo);
					layer.close(index);
				}
			});
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "会员新增";
			vm.tcMember = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.tcMember.id == null ? "sys/tcmember/save" : "sys/tcmember/update";
				var memberSex;
				var memberType;

				var typetext = $("#dropdownMenu").text();
				if(typetext == "男"){
					memberSex ="MALE";
				}else {
					memberSex ="FEMALE";
				}


				var typetext2 = $("#dropdownMenu3").text();
				if(typetext2 == "游客"){
					memberType ="GUEST";
				}else if(typetext2 == "普通会员"){
					memberType ="REGULAR_MEMBER";
				}else{
					memberType ="VERIFIED_MEMBER";
				}
				vm.tcMember.memberType = memberType;
				vm.tcMember.memberSex = memberSex;
                if(vm.tcMember.id == null ){

					vm.tcMember.useStatus ="NORMAL";
					vm.tcMember.memberStatus = "REGISTER_SUCCESS";
					vm.tcMember.subscribeStatus = "NOT_SUBSCRIBE";
					vm.tcMember.partnerProperty="NOT_PARTNER";

				}

                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.tcMember),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/tcmember/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
        apl: function(event){

            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var status1 =$("#jqGrid tr[class*='success']").find("td:eq(10) span").text();
            var status2 = $("#jqGrid tr[class*='success']").find("td:eq(6) span").text();
            var phone = $("#jqGrid tr[class*='success']").find("td:eq(2)").text();
            if(status1=="会员认证成功" && status2=="认证会员"){
                layer.alert("会员认证成功，无须认证");
            }else if(status1=="后台认证失败" && status2=="普通会员"){
                layer.alert("后台认证失败，无须认证");
            }else if(status1=="游客" && status2=="游客"){
                layer.alert("游客，无须认证");
            }else {
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 1,
                    area: '400px;',
                    shade: 0.8,
                    id: 'LAY_layuipro',
                    moveType: 1,
                    content:'<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">是否认证会员</div>',
                    btn: ['是','否'],
                    btnAlign: 'c',
                    btn1: function (index) {
                        var data = "statu1=VERIFY_SUCCESS&statu2=VERIFIED_MEMBER&id="+id+"&phone="+phone+"&smstext=您好，您的会员认证成功！ 【领航社区】";
                        $.ajax({
                            type: "POST",
                            url: baseURL +"sys/tcmember/shenhe",
                            data: data,
                            dataType: "json",
                            success: function(result){
                                if(result.code == 0){
                                    layer.close(index);
                                    layer.alert('会员认证成功', function(index){
                                        location.reload();
                                    });
                                }else{
                                    layer.alert(result.msg);
                                }
                            }
                        });
                    },
                    btn2: function (index) {
                        var data = "statu1=BACKGROUND_VERIFY_FAIL&statu2=REGULAR_MEMBER&id="+id+"&phone="+phone+"&smstext=您好，您的会员认证失败！ 【领航社区】";
                        $.ajax({
                            type: "POST",
                            url:  baseURL +"sys/tcmember/shenhe",
                            data: data,
                            dataType: "json",
                            success: function(result){
                                if(result.code == 0){
                                    layer.close(index);
                                    layer.alert('后台认证失败', function(index){
                                        location.reload();
                                    });
                                }else{
                                    layer.alert(result.msg);
                                }
                            }
                        });
                    }
                });
            }
            //vm.showList = false;
            //vm.title = "审核";
        },
		out:function(){
			$.ajax({
				type: "POST",
				url: baseURL +"sys/tcmember/exportExcel",
				dataType: "json",
				success: function(result){
					if(result.code == 0){
						layer.close(index);
						layer.alert('会员认证成功', function(index){
							location.reload();
						});
					}else{
						layer.alert(result.msg);
					}
				}
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/tcmember/info/"+id, function(r){
                vm.tcMember = r.tcMember;

				if(vm.tcMember.memberSex == "MALE"){
					$("#dropdownMenu").text("男");
				}else{
					$("#dropdownMenu").text("女");
				}

				if(vm.tcMember.memberType == "VERIFIED_MEMBER"){
					$("#memberType li[id='rz']").remove();
					$("#memberType").html($("#memberType").html()+"<li id='rz'><a href='#' onclick='shows3($(this).text())'>认证会员</a></li>");
					$("#dropdownMenu3").text("认证会员");
				}else if(vm.tcMember.memberType == "REGULAR_MEMBER"){
					$("#memberType li[id='rz']").remove();
					$("#dropdownMenu3").text("普通会员");
				}else{
					$("#memberType li[id='rz']").remove();
					$("#dropdownMenu3").text("游客");
				}
            });

		},
		reload: function (event) {
			var typetext = $("#dropdownMenu2").text();
			vm.q.memberName = $("#memberName").val();
			if(typetext == "全部会员信息"){
				vm.q.typeNo = null;
			}else if(typetext == "游客"){
				vm.q.typeNo = "GUEST";
			}else if(typetext == "普通会员"){
				vm.q.typeNo = "REGULAR_MEMBER";
			}else {
				vm.q.typeNo = "VERIFIED_MEMBER";
			}

			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'typeNo':vm.q.typeNo,'membername':vm.q.memberName},
                page:page
            }).trigger("reloadGrid");
		}
	}
});